package com.ctgu.admin.system.controller;

import com.ctgu.admin.system.domain.Article;
import com.ctgu.admin.system.service.ArticleService;
import com.ctgu.component.shiro.ShiroUtil;
import com.ctgu.modules.system.domain.User;
import com.ctgu.modules.system.service.UserService;
import io.swagger.models.auth.In;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/userPage")
public class UserPageController {
    @Autowired
    private UserService userService;
    @Autowired
    private ArticleService articleService;

    //我的账号界面的首页
    @GetMapping("/index")
    //@RequiresPermissions("user")  //更新权限方式
    public String index(Model model){
        //从缓存中导入用户
        User user = ShiroUtil.getSubject();
        model.addAttribute("user", user);
        return "/userPage/myPage";
    }

    @GetMapping("/myPassword")
    public String toMyPassword(Model model){
        User user = ShiroUtil.getSubject();
        model.addAttribute("user", user);
        return "/userPage/myPassword";
    }

    //前往管理我的文章界面
    @GetMapping("/myArticle")
    public String toMyArticle(Model model){
        //从缓存中导入用户
        User user = ShiroUtil.getSubject();
        model.addAttribute("user", user);
        //user = userService.getById(userID);
        //System.out.println("准备获取投稿列表");

        //统计该用户所有的文章中，各种文章的个数
        //List<Integer> articleCount = new ArrayList<>();
        //List<Integer> articleCount = articleService.getArticleClassCntFromUserID(user.getId());//获取每种文章个数


        if(user==null){
            return "system/main/error";
        }else{
            List<Article> articleList = articleService.getUserArticles(user);
            model.addAttribute("articleList", articleList);
            //System.out.println("已打印作者："+user.getNickname()+"的文章列表");
            //System.out.println(articleList);

            //统计各种类型文章的数量
            int[] count = new int[6];
            for (Article article:articleList) {
                if(article.getArticleClass() == null){
                    count[5]++;
                    continue;
                }
                int k = article.getArticleClass();
                count[k]++;
            }
            model.addAttribute("count", count);

            return "/userPage/myArticle";
        }
    }

    @GetMapping("/myPage")
    public String toMyPage(Model model){
        //从缓存中导入用户
        User user = ShiroUtil.getSubject();
        model.addAttribute("user", user);
        if(user==null){
            return "system/main/error";
        }else{
            List<Article> articleList = articleService.getUserArticles(user);
            model.addAttribute("articleList", articleList);
            return "/userPage/myPage";
        }
    }
}
